package com.hxd.otc.module.my.presenter;

import android.support.annotation.NonNull;

import com.hxd.otc.base.BaseBean;
import com.hxd.otc.base.BaseRecyclerPresenter;
import com.hxd.otc.common.enums.BankType;
import com.hxd.otc.module.my.adapter.BankCardAdapter;
import com.hxd.otc.module.my.bean.BankCardBean;
import com.hxd.otc.module.my.bean.UserStatusBean;
import com.hxd.otc.module.my.contract.BankCardContract;
import com.hxd.otc.module.my.model.BankModel;
import com.hxd.otc.module.my.model.UserModel;
import com.hxd.otc.net.BaseObserver;

import java.util.ArrayList;

/**
 * Created by lzy on 2018/6/7 0007.
 * description ${收款方式Presenter}
 */
public class BankCardPresenter extends BaseRecyclerPresenter<BankCardContract.View, BankCardAdapter, BankCardBean> implements BankCardContract.Presenter {

    private final BankModel mBankModel;
    private final UserModel mUserModel;

    public BankCardPresenter(BankCardContract.View view) {
        super(view);
        mBankModel = BankModel.getInstance();
        mUserModel = UserModel.getInstance();
    }

    @Override
    public void loadData(final boolean isRefresh) {
        mBankModel.getBankCardList(new BaseObserver<ArrayList<BankCardBean>>() {
            @Override
            public void onNext(String msg, ArrayList<BankCardBean> bankCardBeans) {
                onLoadDataSuccess(isRefresh, bankCardBeans);
            }

            @Override
            public void onError(int errType, String errMessage) {
                onLoadDataFail(isRefresh, errMessage);
            }
        }, mView.<BaseBean<ArrayList<BankCardBean>>>bindAutoDispose());
    }

    @Override
    public void onDeleteTradeMode(final int position, final BankCardBean item) {
        BankType bankType = BankType.getBankTypeByValue(item.getType());
        BaseObserver baseObserver = new BaseObserver<Object>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull Object o) {
                mView.onDeleteTradeModeSuccess(position, item);
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.showToast(errMessage);
            }
        };
        if (bankType == BankType.BANK) {
            mBankModel.updateBank(item.getId(), item.getPayNo(), item.getBankName(), item.getBranchBankName(), item.getCountry(),
                    0, item.getRealName(), item.getSupportCurrencyCode(), baseObserver, mView.<BaseBean>bindAutoDispose());
        } else {
            mBankModel.updateAlipayOrWinXin(bankType, item.getId(), item.getPayNo(), item.getQrcodeId(), item.getCountry(),
                    0, item.getRealName(), baseObserver, mView.<BaseBean>bindAutoDispose());
        }
    }

    @Override
    public void findCardStatus() {
        mUserModel.findCardStatus(new BaseObserver<UserStatusBean>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull UserStatusBean userStatusBean) {
                mView.findCardStatusSuccess(userStatusBean);
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.findCardStatusFail(errMessage);
            }
        }, mView.<BaseBean<UserStatusBean>>bindAutoDispose());
    }

}
